package com.project.maven.demo_maven.action;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.project.maven.demo_maven.entry.LoginEntity;
import com.project.maven.demo_maven.entry.ResponseEntry;
import com.project.maven.demo_maven.service.LoginService;
import com.project.maven.demo_maven.service.RegisterService;
import com.project.maven.demo_maven.utils.StringUtil;
import com.project.maven.demo_maven.utils.UUIDUtil;


/**
 * 
 * 注册Action
 * @author luzhenbang
 *
 */
@RestController
public class RegisterAction {
    @Autowired
    RegisterService  registerService;
	/**
	 * 注册
	 * @return
	 */
	@RequestMapping("/register")
	public ResponseEntry register(@RequestParam("phone") String phone,
    		@RequestParam("password") String password){
		ResponseEntry entry = new ResponseEntry();
		LoginEntity loginEntity = new LoginEntity();
    	boolean isEmp = false;
    	if (StringUtil.isEmpty(phone)) {
    		isEmp = true;
    		entry.setMessage("手机不能为空");
    		entry.setCode(ResponseEntry.PAREM_EXCEPTION);
		}
    	if (StringUtil.isEmpty(password)) {
    		isEmp = true;
    		entry.setMessage("密码不能为空");
    		entry.setCode(ResponseEntry.PAREM_EXCEPTION);
		}
    	if(!isEmp){
    		loginEntity.setPhone(phone);
    		loginEntity.setPassword(password);
    		loginEntity.setToken(UUIDUtil.getUUID());
    		int result = registerService.register(loginEntity);
    		if(result == -1){
    			entry.setMessage("注册失败");
    			entry.setCode(ResponseEntry.DATA_EXCEPTION);
    		}
    	}
    	
		return entry;
	}
	

	/**
	 * 注册
	 * @return
	 */
	@RequestMapping("/registerBatch")
	public ResponseEntry registerBatch(@RequestBody String body){
		ResponseEntry entry = new ResponseEntry();
    	if (StringUtil.isEmpty(body)) {
    		entry.setMessage("参数不能为空");
    		entry.setCode(ResponseEntry.DATA_EXCEPTION);
		}else {
			ArrayList<LoginEntity> list =  null;
			Gson gson = new Gson();
			Type type = new TypeToken <ArrayList<LoginEntity>>(){}.getType();
			list = gson.fromJson(body, type);
			if(null == list || list.size() <= 0){
				entry.setMessage("数据异常");
	    		entry.setCode(ResponseEntry.DATA_EXCEPTION);
			}else{
				int result = registerService.registerBatch(list);
	    		if(result == -1){
	    			entry.setMessage("注册失败");
	    			entry.setCode(ResponseEntry.DATA_EXCEPTION);
	    		}
			}
			
		}
		return entry;
	}
	
	
	@RequestMapping(value = "/updateBatch" , method = RequestMethod.POST)
	public ResponseEntry updateBatch(@RequestBody String reequestBody){
		ResponseEntry entry = new ResponseEntry();
    	if (StringUtil.isEmpty(reequestBody)) {
    		entry.setMessage("参数不能为空");
    		entry.setCode(ResponseEntry.DATA_EXCEPTION);
		}else {
			ArrayList<LoginEntity> list =  null;
			Gson gson = new Gson();
			Type type = new TypeToken <ArrayList<LoginEntity>>(){}.getType();
			list = gson.fromJson(reequestBody, type);
			if(null == list || list.size() <= 0){
				entry.setMessage("数据异常");
	    		entry.setCode(ResponseEntry.DATA_EXCEPTION);
			}else{
				int result = registerService.updateBatch(list);
	    		if(result <= 0){
	    			entry.setMessage("注册失败");
	    			entry.setCode(ResponseEntry.DATA_EXCEPTION);
	    		}
			}
			
		}
		return entry;
	}
	
	/**
	 * 批量删除
	 * @param reequestBody
	 * @return
	 */
	@RequestMapping(value = "/deleteBatch" , method = RequestMethod.POST)
	public ResponseEntry deleteBatch(@RequestBody String reequestBody){
		ResponseEntry entry = new ResponseEntry();
    	if (StringUtil.isEmpty(reequestBody)) {
    		entry.setMessage("参数不能为空");
    		entry.setCode(ResponseEntry.DATA_EXCEPTION);
		}else {
			ArrayList<LoginEntity> list =  null;
			Gson gson = new Gson();
			Type type = new TypeToken <ArrayList<LoginEntity>>(){}.getType();
			list = gson.fromJson(reequestBody, type);
			if(null == list || list.size() <= 0){
				entry.setMessage("数据异常");
	    		entry.setCode(ResponseEntry.DATA_EXCEPTION);
			}else{
				int result = registerService.deleteBatch(list);
	    		if(result <= 0){
	    			entry.setMessage("注册失败");
	    			entry.setCode(ResponseEntry.DATA_EXCEPTION);
	    		}
			}
			
		}   
		return entry;
	}
	
	
	
}
